from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.ClassAdmincontroller import ClassAdminController
from controller.PublicAdminController import PublicAdminController
from schema.QuerySchema import QueryBase, ClassQuery, ClassByStatusListQuery, ClassByStatusAndPageQuery
from schema.TeachingSchema import ClassInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

class_admin_router = APIRouter()


@class_admin_router.post("/class/add", summary='添加班级信息')
def add_class(classInfo: ClassInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/class/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/class/add', db):
            response = ClassAdminController.add_class(classInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@class_admin_router.post("/class/mod", summary='根据班级id修改班级信息')
def mod_class(classInfo: ClassInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/class/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/class/mod', db):
            response = ClassAdminController.mod_class(classInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@class_admin_router.get("/class/query/allstudents", summary='查询所有班级包含的全部学生信息')
def query_class_all(db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/class/query/allstudents')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/class/query', db):
            response = ClassAdminController.query_class_all_students(db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@class_admin_router.post("/class/querybypage", summary='分页查询所有班级信息')
def query_class_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/class/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/class/query', db):
            response = ClassAdminController.query_class_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@class_admin_router.post("/class/query", summary='根据class_type、course_id、status字段分页查询班级信息')
def query_class_by_field(classQuery: ClassQuery, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/class/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/class/query', db):
            response = ClassAdminController.query_class_by_field(classQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@class_admin_router.post("/class/query/allbystatus/", summary='根据status字段查询班级信息')
def query_class_all_by_status(classByStatusListQuery: ClassByStatusListQuery, db: Session = Depends(get_db_session),
                              result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/class/query/allbystatus')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/class/query', db):
            response = ClassAdminController.query_class_all_by_status(classByStatusListQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@class_admin_router.post("/class/query/allbystatusandpage/", summary='根据status字段分页查询班级信息')
def query_class_all_by_status_and_page(classByStatusAndPageQuery: ClassByStatusAndPageQuery,
                                       db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/class/query/allbystatusandpage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/class/query', db):
            response = ClassAdminController.query_class_all_by_status_and_page(classByStatusAndPageQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
